package com.findchen.map;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocation;

import java.util.Map;
import android.util.Log;


public class MapModule extends ReactContextBaseJavaModule {
  // 初始化
  private AMapLocationClient locationClient = null;
  private AMapLocationClientOption locationOption = null;

  public MapModule(ReactApplicationContext reactContext) {
    super(reactContext);
    locationClient = new AMapLocationClient(reactContext);
    locationOption = new AMapLocationClientOption(); 
  }

  @Override
  public String getName() {
    return "Map";
  }

  @Override
  public void initialize() {
  }

  @ReactMethod
  public void getPosition(final Promise promise) {
    //声明AMapLocationClient类对象
    // AMapLocationClient mLocationClient = new AMapLocationClient(this.getApplicationContext());
    // AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
    //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
    locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
    //获取一次定位结果：
    //该方法默认为false。
    locationOption.setOnceLocation(true);
    //获取最近3s内精度最高的一次定位结果：
    //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
    locationOption.setOnceLocationLatest(true);
    //设置是否返回地址信息（默认返回地址信息）
    locationOption.setNeedAddress(true);
    //缓存机制
    locationOption.setLocationCacheEnable(true);


    //声明定位回调监听器
    //public AMapLocationListener mLocationListener = new AMapLocationListener();
    //初始化定位
    //mLocationClient = new AMapLocationClient(getApplicationContext());
    //设置定位回调监听
    //
    //可以通过类implement方式实现AMapLocationListener接口，也可以通过创造接口类对象的方法实现
    //以下为后者的举例：
    AMapLocationListener mAMapLocationListener = new AMapLocationListener(){
      @Override
      public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
              WritableMap map = Arguments.createMap();
              //可在其中解析amapLocation获取相应内容。
              map.putDouble("longitude", amapLocation.getLongitude());
              map.putDouble("latitude", amapLocation.getLatitude());
              map.putString("address", amapLocation.getAddress());
              map.putString("city", amapLocation.getCity());
              map.putString("aoi", amapLocation.getAoiName());
              map.putString("citycode", amapLocation.getCityCode());
              map.putString("adcode", amapLocation.getAdCode());
              promise.resolve(map);
            }else {
              // //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
              // Log.e("AmapError","location Error, ErrCode:"
              //     + amapLocation.getErrorCode() + ", errInfo:"
              //     + amapLocation.getErrorInfo());
              promise.reject("Amap Error, ErrCode:"
                  + amapLocation.getErrorCode() + ", errInfo:"
                  + amapLocation.getErrorInfo());
            }
        }        
      }
    };
    locationClient.setLocationListener(mAMapLocationListener);
     //给定位客户端对象设置定位参数
    locationClient.setLocationOption(locationOption);
    //启动定位
    locationClient.startLocation();
  }
}
